#!/user/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np
from numpy.random import randn

# 各种特殊数组
a = np.zeros((6, 7))
print(a)  # 全0数组

b = np.ones((6, 7))
print(b)  # 全1数组

c = np.empty((6, 7))
print(c)  # 空数组

d = np.eye(5)
print(d)  # 单位矩阵

e = np.random.randn(3, 4)
print(e)  # 正态分布矩阵

f = np.random.randint(5, 20, (3, 4))
print(f)  # 随机整数矩阵

# 数据类型转换
arr = np.array([1, 2, 3, 4, 5])
print(arr.dtype)
f_arr = arr.astype(np.float64)
print(f_arr.dtype)

# 自定义数据类型
t = np.dtype([('name', 'S20'), ('age', 'i1'), ('mark', 'f4')])
item = np.array([('Mark', 18, 78), ('Juli', 19, 90)], dtype=t)
print(item[1])

# 多维数组的切片和索引
b = np.arange(24).reshape(2, 3, 4)
print(b)

print(b[0, 0, 0])
print(b[:, 0, 0])
print(b[0, 1])
print(b[0, 1, ::2])
print(b[..., 1])
print(b[0, ::-1, -1])
print(b[0, ::2, -1])
print(b[::-1])
s = slice(None, None, -1)
print(b[(s, s, s)])

# 布尔索引
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(7, 4)
print(names)
print(data)

print(data[names == 'Bob', 3])

mask = (names == 'Bob') | (names == 'Will')
print(mask)
print(data[mask])

data[data < 0] = 0
print(data)

# 花式索引
arr = np.empty((8, 4))
for i in range(8):
    arr[i] = i
print(arr)

print(arr[[4, 3, 0, 6]])

print(arr[[-3, -5, -7]])

arr = np.arange(32).reshape((8, 4))
print(arr)
print(arr[[1, 5, 7, 2], [0, 3, 1, 2]])

print(arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]])
print(arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])])

a = np.arange(9).reshape(3, 3)
print(a)
b = 2 * a
print(b)

print(np.hstack((a, b)))
print(np.concatenate((a, b), axis=1))

print(np.vstack((a, b)))
print(np.concatenate((a, b), axis=0))

print(np.dstack((a, b)))

# 数组的分割
a = np.arange(9).reshape(3, 3)
print(a)

print(np.hsplit(a, 3))
print(np.split(a, 3, axis=1))

print(np.vsplit(a, 3))
print(np.split(a, 3, axis=0))